package com.ajx.ablibrary.statistics;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.ajx.ablibrary.R;
import com.umeng.analytics.MobclickAgent;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * Created by lai on 2017/9/7.
 * 友盟统计 说明
 * 1  session的统计
 *      在每个Activity的onResume方法中调用 MobclickAgent.onResume(Context),onPause方法中调用 MobclickAgent.onPause(Context)
 * 2  帐号统计
 *      登录时，调用
 *          public static void onProfileSignIn(String ID) ; （自用帐号登录）【用户名】
 *          public static void onProfileSignIn(String Provider, String ID) ; 第三方登录 【来源（微信。。），用户名】
 *      退出进调用
 *          public static void onProfileSignOff();
 * 3 页面的统计
 *      只由Activity构成的应用：完成第一步时已做好所有事情
 *      其它具体看文档
 * 4.混淆：
 *      -keepclassmembers class * {public <init> (org.json.JSONObject);}
 *      -keep public class [您的应用包名].R$*{public static final int *;}
 *      -keepclassmembers enum * {public static **[] values();
 *      public static ** valueOf(java.lang.String);
 * 5.自定义事件统计
 *      自定义事件的代码需要放在Activity里的onResume--onPause之间
 *      统计发生次数：MobclickAgent.onEvent(Context context, String eventId);
 *      统计点击行为各属性被触发的次数：MobclickAgent.onEvent(Context context, String eventId, HashMap map);
 }
 */

public class UmengGuideActivity extends AppCompatActivity{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void onResume() {
        super.onResume();
        //MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        //MobclickAgent.onPause(this);
    }
    public static boolean checkPermission(Context context, String permission) {
        boolean result = false;
        if (Build.VERSION.SDK_INT >= 23) {
            try {
                Class<?> clazz = Class.forName("android.content.Context");
                Method method = clazz.getMethod("checkSelfPermission", String.class);
                int rest = (Integer) method.invoke(context, permission);
                if (rest == PackageManager.PERMISSION_GRANTED) {
                    result = true;
                } else {
                    result = false;
                }
            } catch (Exception e) {
                result = false;
            }
        } else {
            PackageManager pm = context.getPackageManager();
            if (pm.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
                result = true;
            }
        }
        return result;
    }

    public static String getDeviceInfo(Context context) {
        try {
            org.json.JSONObject json = new org.json.JSONObject();
            android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            String device_id = null;
            if (checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
                device_id = tm.getDeviceId();
            }
            String mac = null;
            FileReader fstream = null;
            try {
                fstream = new FileReader("/sys/class/net/wlan0/address");
            } catch (FileNotFoundException e) {
                fstream = new FileReader("/sys/class/net/eth0/address");
            }
            BufferedReader in = null;
            if (fstream != null) {
                try {
                    in = new BufferedReader(fstream, 1024);
                    mac = in.readLine();
                } catch (IOException e) {
                } finally {
                    if (fstream != null) {
                        try {
                            fstream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            json.put("mac", mac);
            if (TextUtils.isEmpty(device_id)) {
                device_id = mac;
            }
            if (TextUtils.isEmpty(device_id)) {
                device_id = android.provider.Settings.Secure.getString(context.getContentResolver(),
                        android.provider.Settings.Secure.ANDROID_ID);
            }
            json.put("device_id", device_id);
            return json.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
